<?php
abstract class System_App_Model_Abstract extends System_App_Noyau{	
    function __construct( $id = null ){
        if( $id ){
            $this->getEntity( $id );
        }else{
            $this->setId(0);
        }
    }

    public function getEntity( $id ){
        $query = 'SELECT * FROM ' . $this->table . ' WHERE id=' . $id . ' LIMIT 1';
        $sql_result = $this->database->getQueryResult( $query, true );
        return $this->populate( $sql_result );
    }

    public function populate( $entity = array() ){
        if( count( $entity ) > 0 ){
            foreach( $entity as $k=>$v ){
                if( method_exists($this, 'set'.ucfirst($k) ) ){
                    //TODO : vérifier les encodage depuis le fichier config
                    $v = utf8_encode($v);
                    eval( '$this->set'.ucfirst($k).'("'.$v.'");' );
                }
            }
            return true;
        }else{
            return false;
        }
    }

    public function save(){
        if( $this->getId() === 0 ){
                // creation
        }else{
                // mise à jour
        }
    }

    public function toArray(){
        return array();
    }	
}
?>